<?php
class Login extends CI_Controller
{
	function index()
	{
		$this->load->helper('url');
		$this->load->helper('form');
		$data['title'] = 'Log In';
		$data['content'] = 'login_form';
		$this->load->view('layout/login', $data);
	}

	function validate_credentials()
	{
		$this->load->library('session');
		$this->load->helper('url');
        $this->load->model('authentication_model');
        $this->load->model('config_model');

		$username = $this->input->post('username');
		$password = $this->input->post('password');

		$authenticated = $this->authentication_model->authenticate($username, $password);

		if($authenticated) // if the user's credentials validated...
		{
            $maintenance_mode = $this->config_model->is_maintenance_mode();

            if ($maintenance_mode)
            {
                $this->load->view('maintenance_mode');
            }
            else
            {
    			$data = array('username' => $username);
    
    			$is_user = $this->authentication_model->is_user ($username);
    			if ($is_user)
    			{
    				$data['is_user'] = true;
    			};
    
    			$is_admin = $this->authentication_model->is_admin ($username);
    			if ($is_admin)
    			{
    				$data['is_admin'] = true;
    			};
    
                $data['zone_type'] = "Ward";
//                $data['zone_type'] = "Division";
                
    			$this->session->set_userdata($data);
//                redirect('electoral_roll/view/1'); // ?? need to improve this
                redirect('electoral_roll/start');
//            redirect('canvassing_sheets/view/14'); // ?? need to improve this
            }
		}
		else // incorrect username or password
		{
			$this->index();
		}
	}

	function logout()
	{
		$this->load->library('session');
		$this->session->sess_destroy();
		$this->index();
	}

}

?>